<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>修改信息</title>
    <link rel="stylesheet" href="/resources/css/amazeui.min.css" />
    <script src="/resources/js/jquery.min.js"></script>
    <style type="text/css">
        #eye{
            position: relative;
            top:-30px;
            right:10px;
            float: right;
        }
        #eye2{
            position: relative;
            top:-30px;
            right:10px;
            float: right;
        }
    </style>
</head>
<body>
<!-- 顶部 -->
<div style="background-color:#82a6f5;height:7%;">
    <a href="/c/centre" target="_parent">
        <button style="background-color:rgba(255,255,255,0);border-radius:25px;border-color:white;margin-top:7px;margin-left:12px;">
            <b style="color:white;"><</b>
        </button>
    </a>
</div>
<!-- 中间 -->
    <div id="center" style="width:100%;height:93%;" class="am-g">
        <form class="am-form tpl-form-line-form">

            <div class="am-form-group">
                <input type="text" onblur="checknu()" class="phonenum" placeholder="请输入电话号码(必填)">
            </div>
            <a id="id_phonenum" style="display:none;"></a>
            <div class="am-form-group">
                <input type="password" class="tpl-form-input" onblur="check_pwd1()" id="pwd" placeholder="请输入密码(必填)">
                <i id="eye" class="am-icon-eye" style="float: right"></i>
            </div>

            <div class="am-form-group">
                <input type="password" class="tpl-form-input" onblur="check_pwd()" id="pwd2" placeholder="请重新输入密码(必填)">
                <i id="eye2" class="am-icon-eye" style="float: right"></i>
            </div>
            <a id="checkpwd" style="display:none;"></a>
            <div class="am-form-group">
                <input type="text" class="tpl-form-input" id="phone_check" placeholder="请输入手机验证码">
            </div>

            <div class="am-form-group tpl-login-remember-me">
                <input type="button" class="am-btn am-btn-warning" value="点击获取验证码" onclick="getphonecheck(this)" ></input>
                <a id="getphonecheck" style="display:none;"></a>
                <!------同意用户注册协议--------->
            </div>
            <a id="message" style="display:none;"></a>
            <div class="am-form-group">
                <button type="button" class="am-btn am-btn-secondary am-btn-block" id="find">找回</button>
            </div>

        </form>
    </div>
</body>
<script>
    var checknum;//验证码接收
    var check_pwd_type=false;
    var check_pwd_type2=false;
    var checknu_type=false;
    var countdown=60;//验证码停留60秒

    //手机号码验证 （1开头，第二位为[345789]之一，长度为11位）
    function checknu(){
        phone = $(".phonenum").val();
        if( !( /^1[345789]\d{9}$/.test(phone) ) ){
            var phonenum=document.getElementById("id_phonenum");
            phonenum.innerHTML="您填写的手机号码有误，请检查重填！";
            phonenum.style.color="red";
            phonenum.style.display="block";
            return false;
        }else {
            var formData = new FormData();
            var url= "/r/isexist";
            formData.append("userTel",phone);
            $.ajax({
                type: "POST",
                cache: false,
                url:url,
                data:formData,
                processData: false,
                contentType: false
            }).done(
                function (res) {
                    if (res.emp==false){
                        checknu_type=true;
                        var phonenum=document.getElementById("id_phonenum");
                        phonenum.setAttribute("style","display:none");
                    }else {
                        checknu_type=false;
                        var phonenum=document.getElementById("id_phonenum");
                        phonenum.innerHTML="该用户不存在";
                        phonenum.style.color="red";
                        phonenum.style.display="block";
                    }
                }
            ).error(function () {
                alert("启用失败");
            });
        }
        return checknu_type;
    }
    //--------密码显示与隐藏-------------
    $("#eye").on("click",function(){
        if($("#eye").attr("class") == "am-icon-eye"){
            $("#eye").removeClass().addClass("am-icon-eye-slash");
            $("#pwd").attr("type","text");
        }else{
            $("#eye").removeClass().addClass("am-icon-eye");
            $("#pwd").attr("type","password");
        }
    });
    $("#eye2").on("click",function(){
        if($("#eye2").attr("class") == "am-icon-eye"){
            $("#eye2").removeClass().addClass("am-icon-eye-slash");
            $("#pwd2").attr("type","text");
        }else{
            $("#eye2").removeClass().addClass("am-icon-eye");
            $("#pwd2").attr("type","password");
        }
    });
    //密码是否相同校验
    function check_pwd() {
        if ($("#pwd").val()!=$("#pwd2").val()) {
            var checkpwd=document.getElementById("checkpwd");
            checkpwd.innerHTML="两次密码不统一";
            checkpwd.style.color="red";
            checkpwd.style.display="block";
            check_pwd_type=false;
        }else {
            if($("#pwd").val()=="" || $("#pwd2").val()==""){
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.innerHTML="密码不能为空";
                checkpwd.style.color="red";
                checkpwd.style.display="block";
                check_pwd_type=false;
            }else {
                check_pwd_type=true;
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.setAttribute("style","display:none");
            }
        }
    }
    //
    function check_pwd1() {
        if ($("#pwd").val()!=""){
            if($("#pwd").val().length<6){
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.innerHTML="密码长度不能小于6";
                checkpwd.style.color="red";
                checkpwd.style.display="block";
                check_pwd_type2=false;
            }else{
                check_pwd_type2=true;
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.setAttribute("style","display:none");
            }
        } else {
            var checkpwd=document.getElementById("checkpwd");
            checkpwd.innerHTML="密码不能为空";
            checkpwd.style.color="red";
            checkpwd.style.display="block";
            check_pwd_type2=false;
        }
    }
    // //----------------获取手机验证码-------------------
    function getphonecheck(obj) {
        var chechnu_type2=checknu();
        if (check_pwd_type&&check_pwd_type2){
            if (checknu_type&&chechnu_type2){
                //倒计时------------------------------
                if (countdown==59){
                    //手机号码验证 （1开头，第二位为[345789]之一，长度为11位）
                    phone = $(".phonenum").val();
                    if( !( /^1[345789]\d{9}$/.test(phone) ) ){
                        var message=document.getElementById("message");
                        message.innerHTML="您填写的手机号码有误，请检查重填！";
                        message.style.color="red";
                        message.style.display="block";
                        return false;
                    }
                    var formData=new FormData();
                    formData.append("userTel",$(".phonenum").val());
                    $.ajax({
                        type: "POST",
                        cache: false,
                        url:"/r/message",
                        data:formData,
                        processData: false,
                        contentType: false
                    }).done(
                        function (res) {
                            if (res.emp==true){
                                var getphonecheck=document.getElementById("getphonecheck");
                                getphonecheck.innerHTML="短信已发送请注意查收";
                                getphonecheck.style.color="red";
                                getphonecheck.style.display="block";
                                remv=self.setInterval("remv()",1000);//删除短信提示
                                checknum=res.message;
                                int=self.setInterval("remvchecknum()",60000);
                                //隐藏密码提示
                                var checkpwd=document.getElementById("checkpwd");
                                checkpwd.setAttribute("style","display:none");
                            }
                        }
                    ).error(function () {
                        alert("请求失败");
                    });
                }
                if (countdown == 0) {
                    obj.removeAttribute("disabled");
                    obj.value="免费获取验证码";
                    countdown = 60;
                    return;
                } else {
                    obj.setAttribute("disabled", true);
                    obj.value="重新发送(" + countdown + ")";
                    countdown--;
                }
                setTimeout(function() {
                        getphonecheck(obj) }
                    ,1000)
            }else {
                if ($(".phonenum").val()==""){
                    var message=document.getElementById("message");
                    message.innerHTML="密码不能为空";
                    message.style.color="red";
                    message.style.display="block";
                }else if( !( /^1[345789]\d{9}$/.test(phone) ) ){
                    var message=document.getElementById("message");
                    message.innerHTML="您填写的手机号码有误，请检查重填！";
                    message.style.color="red";
                    message.style.display="block";
                    return false;
                }else{
                    //--------------------------------------
                    var message=document.getElementById("message");
                    message.innerHTML="号码已经被注册";
                    message.style.color="red";
                    message.style.display="block";
                }
            }
        } else if ($("#pwd").val()=="" || $("#pwd2").val()==""){
            var message=document.getElementById("message");
            message.innerHTML="密码不能为空";
            message.style.color="red";
            message.style.display="block";
        } else if (!check_pwd_type2) {
            var checkpwd=document.getElementById("checkpwd");
            checkpwd.innerHTML="密码长度不能小于6";
            checkpwd.style.color="red";
            checkpwd.style.display="block";
            check_pwd_type2=false;
        }else {
            var checkpwd=document.getElementById("checkpwd");
            checkpwd.innerHTML="两次密码不统一";
            checkpwd.style.color="red";
            checkpwd.style.display="block";
        }
    }
    //-------找回------
    $(document).ready(function(e) {
        $("#find").on("click", function () {
            if (check_pwd_type&&check_pwd_type2) {
                if (checknu_type) {
                    // 检查手机验证码是否正确
                    if ($("#phone_check").val() != checknum) {
                        if ($("#phone_check").val() == "") {
                            var message = document.getElementById("message");
                            message.innerHTML = "请填写验证码";
                            message.style.color = "red";
                            message.style.display = "block";
                        } else {
                            var message = document.getElementById("message");
                            message.innerHTML = "手机验证码有误";
                            message.style.color = "red";
                            message.style.display = "block";
                        }
                        return false;
                    }
                    if ($(".phonenum").val() == "" || $("#pwd").val() == "" || $("#phone_check").val() == "" || $("#pwd2").val() == "") {
                        var message = document.getElementById("message");
                        message.innerHTML = "请输入完整信息";
                        message.style.color = "red";
                        message.style.display = "block";
                        return false;
                    }
                    var message = document.getElementById("message");
                    message.setAttribute("style", "display:none");
                    // $("#checkphone_div").hide();
                    // $("#signup_div").show();
                    //找回
                    var formData = new FormData();
                    formData.append("userTel",$(".phonenum").val());
                    formData.append("password",$("#pwd").val());
                    var url="/f/modifyPwd";
                    $.ajax({
                        type: "POST",
                        cache: false,
                        url:url,
                        data:formData,
                        processData: false,
                        contentType: false
                    }).done(
                        function (res) {
                            if (res.emp==true){
                                $(".phonenum").val("");
                                $("#pwd").val("");
                                alert("找回成功");
                                window.location.href="/l/login";
                            }else if (res.emp==false) {
                                alert("找回失败");
                            }else {
                                alert("数据异常");
                            }
                        }
                    ).error(function () {
                        alert("启用失败");
                    });

                }
            } else if ($("#pwd").val() == "" || $("#pwd2").val() == "") {
                var checkpwd = document.getElementById("checkpwd");
                checkpwd.innerHTML = "密码不能为空";
                checkpwd.style.color = "red";
                checkpwd.style.display = "block";
            } else if (!check_pwd_type2) {
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.innerHTML="密码长度不能小于6";
                checkpwd.style.color="red";
                checkpwd.style.display="block";
                check_pwd_type2=false;
            }else {
                var checkpwd = document.getElementById("checkpwd");
                checkpwd.innerHTML = "两次密码不统一";
                checkpwd.style.color = "red";
                checkpwd.style.display = "block";
            }
        });

    });
    //-------------------删除前端验证码-------------------------
    function remvchecknum() {
        checknum="";
        window.clearInterval(int);
    }
    //删除短信提示信息
    function remv() {
        var getphonecheck=document.getElementById("getphonecheck");
        getphonecheck.setAttribute("style","display:none");
    }
</script>
</html>